package ar.camera.globject;

import java.io.IOException;
import java.io.InputStream;
import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.nio.FloatBuffer;

import javax.microedition.khronos.opengles.GL10;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Bitmap.Config;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.opengl.GLUtils;
import ar.activity.R;
import ar.camera.pojo.GLLocation;

public class Intersection {

	private FloatBuffer vertexBuffer;
	private boolean isLoadedTexture;
	
	public boolean isLoadedTexture() {
		return isLoadedTexture;
	}

	private float vertices[];	
	private FloatBuffer textureBuffer;
	private float texture[] = {
			// Mapping coordinates for the vertices
			0.0f, 1.0f, // top left (V2)
			0.0f, 0.0f, // bottom left (V1)
			1.0f, 1.0f, // top right (V4)
			1.0f, 0.0f // bottom right (V3)
	};

	/** The texture pointer */
	private int[] textures = new int[1];

	public Intersection(GLLocation l1, GLLocation l2) {
		vertices = new float[12];
		vertices[1] = vertices[4] = vertices[7] = vertices[10] = 0f;

		float deltaX = 1f;
		float deltaZ = 1f;

		vertices[0] = vertices[3] = l1.x - deltaX;
		vertices[2] = vertices[8] = l1.z + deltaZ;
		vertices[6] = vertices[9] = l1.x + deltaX;
		vertices[5] = vertices[11] = l1.z - deltaZ;

		ByteBuffer byteBuffer = ByteBuffer.allocateDirect(vertices.length * 4);
		byteBuffer.order(ByteOrder.nativeOrder());
		vertexBuffer = byteBuffer.asFloatBuffer();
		vertexBuffer.put(vertices);
		vertexBuffer.position(0);

		byteBuffer = ByteBuffer.allocateDirect(texture.length * 4);
		byteBuffer.order(ByteOrder.nativeOrder());
		textureBuffer = byteBuffer.asFloatBuffer();
		textureBuffer.put(texture);
		textureBuffer.position(0);

		isLoadedTexture = false;
	}
	
	public void loadGLTexture(GL10 gl, Context context) {

		InputStream is = context.getResources().openRawResource(
				R.drawable.route_intersection);
		Bitmap bitmap;
		try {
			bitmap = BitmapFactory.decodeStream(is);
		} finally {
			try {
				is.close();
			} catch (IOException e) {
			}
		}
		Config cfg = bitmap.getConfig();
		if (cfg == null)
			cfg = Bitmap.Config.ARGB_8888;
		Bitmap bmOverlay = Bitmap.createBitmap(bitmap.getWidth(),
				bitmap.getHeight(), cfg);

		Canvas canvas = new Canvas(bmOverlay);
		Paint paint = new Paint();
		paint.setColor(Color.CYAN);
		paint.setFlags(Paint.ANTI_ALIAS_FLAG);
		canvas.drawBitmap(bitmap, 0, 0, paint);

		gl.glGenTextures(1, textures, 0);
		gl.glBindTexture(GL10.GL_TEXTURE_2D, textures[0]);

		gl.glTexParameterf(GL10.GL_TEXTURE_2D, GL10.GL_TEXTURE_MIN_FILTER,
				GL10.GL_NEAREST);
		gl.glTexParameterf(GL10.GL_TEXTURE_2D, GL10.GL_TEXTURE_MAG_FILTER,
				GL10.GL_LINEAR);

		GLUtils.texImage2D(GL10.GL_TEXTURE_2D, 0, bmOverlay, 0);
		gl.glBlendFunc(GL10.GL_SRC_ALPHA, GL10.GL_ONE_MINUS_SRC_ALPHA);
		gl.glEnable(GL10.GL_BLEND);

		bitmap.recycle();
		isLoadedTexture = true;
	}

	public void draw(GL10 gl) {
		ByteBuffer byteBuffer = ByteBuffer.allocateDirect(vertices.length * 4);
		byteBuffer.order(ByteOrder.nativeOrder());
		vertexBuffer = byteBuffer.asFloatBuffer();
		vertexBuffer.put(vertices);
		vertexBuffer.position(0);

		gl.glBindTexture(GL10.GL_TEXTURE_2D, textures[0]);

		gl.glEnableClientState(GL10.GL_VERTEX_ARRAY);
		gl.glEnableClientState(GL10.GL_TEXTURE_COORD_ARRAY);

		gl.glFrontFace(GL10.GL_CW);

		gl.glVertexPointer(3, GL10.GL_FLOAT, 0, vertexBuffer);
		gl.glTexCoordPointer(2, GL10.GL_FLOAT, 0, textureBuffer);

		gl.glDrawArrays(GL10.GL_TRIANGLE_STRIP, 0, vertices.length / 3);
	}
}